<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Carbon\Carbon;
use App\Models\User;



class SaveGoodsBrowseJobs implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $users;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(User $user , $ttl)
    {
        $this->users = $user;

        // 延时队列 多少秒
        $this->delay($ttl);
    }


    /**
     * $k 当前循环第几条数据
     * $browse_count  一个商品有多少条浏览数据
     * $newData  指定数组里的数据还剩下多少数值
     * $data     指定数组里的数值
     * $inDays   用户注册多少天了
     */
    function H09($k , $browse_count , $newData ,$data){

        if (date('H') == '08'){
            if ($k+1 == $browse_count){
                $ss1 = $newData;
                $ss2 = rand($ss1*2, $ss1*5);
            }else{
                $ss1 = number_format(ceil($data[0] / $browse_count) , 0 , '.' , '') * 2;
                $ss1 = rand(1 , $ss1);
                $ss2 = rand($ss1*2, $ss1*5);
            }
            $newData = $newData - $ss1;
        }else {
            if ($k+1 == $browse_count){
                $ss1 = $newData;
                $ss2 = rand($ss1*2, $ss1*5);
            }else{
                $ss1 = number_format(ceil($data[1] / $browse_count) , 0 , '.' , '') * 2;
                $ss1 = rand(1 , $ss1);
                $ss2 = rand($ss1*2, $ss1*5);
            }
            $newData = $newData - $ss1;
        }
        return [$ss1 , $ss2 , $newData];
    }


    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 数组
        $data = array(array(0=>'227',1=>'201'),array(0=>'207',1=>'311'),array(0=>'235',1=>'353'),
            array(0=>'155',1=>'232'),array(0=>'191',1=>'286'),array(0=>'239',1=>'358'),array(0=>'255',1=>'383'),
            array(0=>'180',1=>'271'),array(0=>'276',1=>'415'),array(0=>'255',1=>'383'),array(0=>'280',1=>'420'),
            array(0=>'214',1=>'322'),array(0=>'295',1=>'443'),array(0=>'357',1=>'535'),array(0=>'255',1=>'383'),
            array(0=>'240',1=>'361'),array(0=>'295',1=>'443'),array(0=>'332',1=>'499'),array(0=>'328',1=>'492'),
            array(0=>'374',1=>'562'),array(0=>'293',1=>'439'),array(0=>'295',1=>'443'),array(0=>'386',1=>'580'),
            array(0=>'429',1=>'643'),array(0=>'548',1=>'823'),array(0=>'513',1=>'769'),array(0=>'592',1=>'889'),
            array(0=>'652',1=>'979'),array(0=>'615',1=>'923'),array(0=>'732',1=>'1099'),array(0=>'653',1=>'979'),
            array(0=>'793',1=>'1189'),array(0=>'733',1=>'1100'),array(0=>'915',1=>'1373'),array(0=>'876',1=>'1314'),
            array(0=>'773',1=>'1160'),array(0=>'792',1=>'1188'),array(0=>'873',1=>'1309'),array(0=>'956',1=>'1434'),
            array(0=>'433',1=>'649'),array(0=>'1055',1=>'1582'),array(0=>'1007',1=>'1511'),array(0=>'1076',1=>'1614'),
            array(0=>'1239',1=>'1858'),array(0=>'1113',1=>'1669'),array(0=>'1157',1=>'1735'),array(0=>'1252',1=>'1879'),
            array(0=>'1209',1=>'1813'),array(0=>'1157',1=>'1735'),array(0=>'1396',1=>'2095'),array(0=>'1132',1=>'1699'),
            array(0=>'1211',1=>'1816'),array(0=>'1316',1=>'1974'),array(0=>'1273',1=>'1909'),array(0=>'1238',1=>'1858'),
            array(0=>'1530',1=>'2296'),array(0=>'1313',1=>'1970'),array(0=>'1132',1=>'1698'),array(0=>'1211',1=>'1816'),
            array(0=>'1312',1=>'1968'),array(0=>'1441',1=>'1868'),array(0=>'1541',1=>'1868'));

        $users = $this->users;  // 用户数据

        // 查询当天浏览数据
        $goods_browse = $users->goods_browse()->whereDate('created_at',now())->inRandomOrder()->get();

        $newData = '';
        foreach ($goods_browse as $k=>$v) {
            if (!in_array(date('H'),['08','18'])){
                //break;
            }
            $InDays = Carbon::parse($users->created_at->toDateString())->diffInDays();// 用户注册多少天了
            $browse_count = $goods_browse->count(); // 一个商品浏览数据有多少条

            if ($InDays <= 60) { //$InDays 小于 60走第一步
                $newData = empty($newData) ? $data[$InDays][0] : $newData;

                list($ss1 , $ss2 ,$newDataS) = $this->H09($k , $browse_count , $newData ,$data[$InDays]);
            }else{
                $newData = empty($newData) ? $data[61][1] : $newData;

                list($ss1 , $ss2 , $newDataS) = $this->H09($k , $browse_count , $newData ,$data[61]);
            }
            $v->increment('pagevies' , $ss1); //浏览量
            $v->increment('show_num' , $ss2); //展现量
            $newData = $newDataS;

            if($newData <= 0){  // 当数值等于0后，跳出这次循环

                exec('php artisan szsmkj:SaveUserBrowseTotal '.$users->id);
                break;
            }
        }
    }
}
